/*
 * MainView.java
 */

package webosdoctorurlgenerator;

import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.text.PlainDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.JOptionPane;
import java.util.TimerTask;
import java.util.Timer;
import java.awt.Desktop;
import java.net.URI;

/**
 * The application's main frame.
 */
public class MainView extends FrameView {
    private final String[] MONTHS = new String[] {"January", "February", "March", "April",
            "May", "June", "July", "August", "September", "October", "November", "December"};
    private final String[] PRE = new String[] {"pre", "p", "p100"};
    private final String[] PIXI = new String[] {"pixi", "px", "p120"};
    private final String[] SPRINT = new String[] {"sr1nt", "ewwsprint"};
    private final String[] BELL = new String[] {"b11e", "ewwbellmo"};
    private final String[] GSM_EU = new String[] {"wre", "ueu-wr"};
    private final String[] TELCEL = new String[] {"tce11", "ewwtelcel"};
    private final String[] GSM_NA = new String[] {"wrna", "una-wr"};
    private String[] device;
    private String[] type;
    private String version;
    private int mm;
    private int dd;
    private int yyyy;
    private Timer t;

    public MainView(SingleFrameApplication app) {
        super(app);
        initComponents();
        device = PRE;
        type = SPRINT;
        t = new Timer();
    }

    private boolean okToGo() {
        boolean result = false;
        if(jRadioButton7.isSelected()){
            result = (jTextField1.getText().length()>0 && jTextField2.getText().length()>0
                    && jTextField3.getText().length()>0 && jTextField4.getText().length()>0);
        } else { //jRadioButton8 is selected
            result = (jTextField1.getText().length()>0 && jTextField5.getText().length()>0
                    && jTextField6.getText().length()>0 && jTextField7.getText().length()>0
                    && jTextField8.getText().length()>0 && jTextField9.getText().length()>0
                    && jTextField10.getText().length()>0);
        }
        return result;
    }
    
    private String buildURL() {
        String result = null;
        try {
            String m = mm + "";
            if(mm<10) {
                m = "0" + m;
            }
            String d = dd + "";
            if(dd<10) {
                d = "0" + d;
            }
            result = "http://palm.cdnetworks.net/rom/" + device[0] + "/" + device[1] +
                    version.replaceAll("\\.", "") + "r0d" + m + d + yyyy + "/" +
                    type[0] + "p" + version.replaceAll("\\.", "") + "rod/webosdoctor" +
                    device[2] + type[1] + ".jar";

        } catch(Exception e) {
            result = null;
        }
        return result;
    }

    private boolean scanForDate() {
        boolean result = false;
        int startMM = Integer.parseInt(jTextField6.getText());
        int startDD = Integer.parseInt(jTextField5.getText());
        int startYYYY = Integer.parseInt(jTextField7.getText());
        int endMM = Integer.parseInt(jTextField8.getText());
        int endDD = Integer.parseInt(jTextField9.getText());
        int endYYYY = Integer.parseInt(jTextField10.getText());

        for(yyyy=startYYYY; yyyy<=endYYYY; yyyy++) {
            for(mm=1; mm<=12; mm++) {
                for(dd=1; dd<=31; dd++) {
                    if(yyyy>=startYYYY && mm>=startMM && dd>=startDD) {
                        String currURL = buildURL();
                        if(currURL!=null) {
                            if(urlExists(currURL)) {
                                return true;
                            }
                        }
                    }
                    if(yyyy==endYYYY && mm==endMM && dd==endDD) {
                        break;
                    }
                }
                if(yyyy==endYYYY && mm==endMM) {
                    break;
                }
            }
        }
        return false;
    }

    private boolean isRangeValid() {
        boolean result = true;
        int startMM = Integer.parseInt(jTextField6.getText());
        int startDD = Integer.parseInt(jTextField5.getText());
        int startYYYY = Integer.parseInt(jTextField7.getText());
        int endMM = Integer.parseInt(jTextField8.getText());
        int endDD = Integer.parseInt(jTextField9.getText());
        int endYYYY = Integer.parseInt(jTextField10.getText());
        if(startYYYY > endYYYY) {
            result = false;
        } else if(startYYYY==endYYYY){
            if(startMM > endMM) {
                result = false;
            } else if(startMM==endMM) {
                if(startDD > endMM) {
                    result = false;
                }
            }
        }
        if(!isValidDate(startDD, startMM, startYYYY)) {
            result = false;
        }
        if(!isValidDate(endDD, endMM, endYYYY)) {
            result = false;
        }
        return result;
    }

    private boolean isValidDate(int day, int month, int year) {
        return (day<=31 && day!=0 && month<=12 && month!=0 && year>999 && year<10000);
    }

    private boolean urlExists(String URLName){
        boolean result = false;
        try {
            HttpURLConnection.setFollowRedirects(false);
            HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
            con.setInstanceFollowRedirects(false);
            con.setRequestMethod("HEAD");
            result = (con.getResponseCode() == HttpURLConnection.HTTP_OK);
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    private void setState(boolean val) {
        if(!val) {
            jButton1.setText("Please wait...");
        } else {
            jButton1.setText("Generate URL");
        }
        jButton1.setEnabled(val);
        jRadioButton1.setEnabled(val);
        jRadioButton2.setEnabled(val);
        jRadioButton3.setEnabled(val);
        jRadioButton4.setEnabled(val);
        jRadioButton5.setEnabled(val);
        jRadioButton6.setEnabled(val);
        jRadioButton7.setEnabled(val);
        jRadioButton8.setEnabled(val);
        jTextField1.setEnabled(val);
        jTextField2.setEnabled(val);
        jTextField3.setEnabled(val);
        jTextField4.setEnabled(val);
        jTextField5.setEnabled(val);
        jTextField6.setEnabled(val);
        jTextField7.setEnabled(val);
        jTextField8.setEnabled(val);
        jTextField9.setEnabled(val);
        jTextField10.setEnabled(val);
        jTextField11.setEnabled(val);
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jRadioButton6 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        jLayeredPane3 = new javax.swing.JLayeredPane();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jLayeredPane4 = new javax.swing.JLayeredPane();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLayeredPane5 = new javax.swing.JLayeredPane();
        jTextField10 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jTextField3 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jTextField7 = new javax.swing.JTextField();
        jRadioButton7 = new javax.swing.JRadioButton();
        jRadioButton8 = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jTextField9 = new javax.swing.JTextField();
        jTextField8 = new javax.swing.JTextField();
        jLayeredPane6 = new javax.swing.JLayeredPane();
        jButton2 = new javax.swing.JButton();
        jTextField11 = new javax.swing.JTextField();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        devicesGroup = new javax.swing.ButtonGroup();
        carriersGroup = new javax.swing.ButtonGroup();
        releaseDateGroup = new javax.swing.ButtonGroup();

        mainPanel.setName("mainPanel"); // NOI18N

        jLayeredPane1.setName("jLayeredPane1"); // NOI18N

        jLayeredPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Type"));
        jLayeredPane2.setName("jLayeredPane2"); // NOI18N

        carriersGroup.add(jRadioButton2);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(webosdoctorurlgenerator.WebOSDoctorURLGeneratorApp.class).getContext().getResourceMap(MainView.class);
        jRadioButton2.setText(resourceMap.getString("jRadioButton2.text")); // NOI18N
        jRadioButton2.setName("jRadioButton2"); // NOI18N
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });
        jRadioButton2.setBounds(10, 50, 100, 23);
        jLayeredPane2.add(jRadioButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        carriersGroup.add(jRadioButton1);
        jRadioButton1.setSelected(true);
        jRadioButton1.setText(resourceMap.getString("jRadioButton1.text")); // NOI18N
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });
        jRadioButton1.setBounds(10, 20, 100, 23);
        jLayeredPane2.add(jRadioButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        carriersGroup.add(jRadioButton5);
        jRadioButton5.setText(resourceMap.getString("jRadioButton5.text")); // NOI18N
        jRadioButton5.setName("jRadioButton5"); // NOI18N
        jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton5ActionPerformed(evt);
            }
        });
        jRadioButton5.setBounds(120, 20, 150, 23);
        jLayeredPane2.add(jRadioButton5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        carriersGroup.add(jRadioButton6);
        jRadioButton6.setText(resourceMap.getString("jRadioButton6.text")); // NOI18N
        jRadioButton6.setName("jRadioButton6"); // NOI18N
        jRadioButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton6ActionPerformed(evt);
            }
        });
        jRadioButton6.setBounds(120, 50, 150, 23);
        jLayeredPane2.add(jRadioButton6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane2.setBounds(130, 10, 280, 90);
        jLayeredPane1.add(jLayeredPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.setBounds(120, 290, 160, 30);
        jLayeredPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Device"));
        jLayeredPane3.setName("jLayeredPane3"); // NOI18N

        devicesGroup.add(jRadioButton3);
        jRadioButton3.setSelected(true);
        jRadioButton3.setText(resourceMap.getString("jRadioButton3.text")); // NOI18N
        jRadioButton3.setName("jRadioButton3"); // NOI18N
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });
        jRadioButton3.setBounds(10, 20, 80, 23);
        jLayeredPane3.add(jRadioButton3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        devicesGroup.add(jRadioButton4);
        jRadioButton4.setText(resourceMap.getString("jRadioButton4.text")); // NOI18N
        jRadioButton4.setName("jRadioButton4"); // NOI18N
        jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton4ActionPerformed(evt);
            }
        });
        jRadioButton4.setBounds(10, 50, 80, 23);
        jLayeredPane3.add(jRadioButton4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane3.setBounds(10, 10, 100, 90);
        jLayeredPane1.add(jLayeredPane3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane4.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Version"));
        jLayeredPane4.setName("jLayeredPane4"); // NOI18N

        jTextField1.setName("jTextField1"); // NOI18N
        jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField1FocusGained(evt);
            }
        });
        jTextField1.setBounds(120, 20, 110, 20);
        jLayeredPane4.add(jTextField1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jLabel1.setBounds(20, 20, 100, 20);
        jLayeredPane4.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane4.setBounds(80, 110, 250, 50);
        jLayeredPane1.add(jLayeredPane4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane5.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Release Date"));
        jLayeredPane5.setName("jLayeredPane5"); // NOI18N

        jTextField10.setText(resourceMap.getString("jTextField10.text")); // NOI18N
        jTextField10.setName("jTextField10"); // NOI18N
        jTextField10.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField10FocusGained(evt);
            }
        });
        jTextField10.setBounds(330, 80, 60, 20);
        jLayeredPane5.add(jTextField10, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N
        jTextField2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField2FocusGained(evt);
            }
        });
        jTextField2.setBounds(100, 60, 60, 20);
        jLayeredPane5.add(jTextField2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator1.setName("jSeparator1"); // NOI18N
        jSeparator1.setBounds(180, 20, 10, 80);
        jLayeredPane5.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextField3.setText(resourceMap.getString("jTextField3.text")); // NOI18N
        jTextField3.setName("jTextField3"); // NOI18N
        jTextField3.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField3FocusGained(evt);
            }
        });
        jTextField3.setBounds(20, 60, 30, 20);
        jLayeredPane5.add(jTextField3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        jLabel2.setBounds(200, 50, 50, 20);
        jLayeredPane5.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextField5.setText(resourceMap.getString("jTextField5.text")); // NOI18N
        jTextField5.setName("jTextField5"); // NOI18N
        jTextField5.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField5FocusGained(evt);
            }
        });
        jTextField5.setBounds(290, 50, 30, 20);
        jLayeredPane5.add(jTextField5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextField6.setText(resourceMap.getString("jTextField6.text")); // NOI18N
        jTextField6.setName("jTextField6"); // NOI18N
        jTextField6.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField6FocusGained(evt);
            }
        });
        jTextField6.setBounds(250, 50, 30, 20);
        jLayeredPane5.add(jTextField6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextField7.setText(resourceMap.getString("jTextField7.text")); // NOI18N
        jTextField7.setName("jTextField7"); // NOI18N
        jTextField7.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField7FocusGained(evt);
            }
        });
        jTextField7.setBounds(330, 50, 60, 20);
        jLayeredPane5.add(jTextField7, javax.swing.JLayeredPane.DEFAULT_LAYER);

        releaseDateGroup.add(jRadioButton7);
        jRadioButton7.setSelected(true);
        jRadioButton7.setText(resourceMap.getString("jRadioButton7.text")); // NOI18N
        jRadioButton7.setName("jRadioButton7"); // NOI18N
        jRadioButton7.setBounds(20, 20, 150, 23);
        jLayeredPane5.add(jRadioButton7, javax.swing.JLayeredPane.DEFAULT_LAYER);

        releaseDateGroup.add(jRadioButton8);
        jRadioButton8.setText(resourceMap.getString("jRadioButton8.text")); // NOI18N
        jRadioButton8.setName("jRadioButton8"); // NOI18N
        jRadioButton8.setBounds(200, 20, 170, 23);
        jLayeredPane5.add(jRadioButton8, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        jLabel3.setBounds(200, 80, 50, 20);
        jLayeredPane5.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextField4.setText(resourceMap.getString("jTextField4.text")); // NOI18N
        jTextField4.setName("jTextField4"); // NOI18N
        jTextField4.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField4FocusGained(evt);
            }
        });
        jTextField4.setBounds(60, 60, 30, 20);
        jLayeredPane5.add(jTextField4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextField9.setText(resourceMap.getString("jTextField9.text")); // NOI18N
        jTextField9.setName("jTextField9"); // NOI18N
        jTextField9.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField9FocusGained(evt);
            }
        });
        jTextField9.setBounds(290, 80, 30, 20);
        jLayeredPane5.add(jTextField9, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextField8.setText(resourceMap.getString("jTextField8.text")); // NOI18N
        jTextField8.setName("jTextField8"); // NOI18N
        jTextField8.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField8FocusGained(evt);
            }
        });
        jTextField8.setBounds(250, 80, 30, 20);
        jLayeredPane5.add(jTextField8, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane5.setBounds(10, 170, 400, 110);
        jLayeredPane1.add(jLayeredPane5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane6.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(resourceMap.getColor("jLayeredPane6.border.border.lineColor")), resourceMap.getString("jLayeredPane6.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jLayeredPane6.border.titleFont"))); // NOI18N
        jLayeredPane6.setName("jLayeredPane6"); // NOI18N

        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jButton2.setBounds(360, 13, 30, 30);
        jLayeredPane6.add(jButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextField11.setFont(resourceMap.getFont("jTextField11.font")); // NOI18N
        jTextField11.setName("jTextField11"); // NOI18N
        jTextField11.setBounds(10, 20, 340, 20);
        jLayeredPane6.add(jTextField11, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane6.setBounds(10, 330, 400, 50);
        jLayeredPane1.add(jLayeredPane6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(webosdoctorurlgenerator.WebOSDoctorURLGeneratorApp.class).getContext().getActionMap(MainView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
        if(jRadioButton3.isSelected()) {
            device = PRE;
        }
    }//GEN-LAST:event_jRadioButton3ActionPerformed

    private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton4ActionPerformed
        if(jRadioButton4.isSelected()) {
            device = PIXI;
        }
    }//GEN-LAST:event_jRadioButton4ActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        if(jRadioButton1.isSelected()) {
            type = SPRINT;
        }
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        if(jRadioButton2.isSelected()) {
            type = BELL;
        }
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton5ActionPerformed
        if(jRadioButton5.isSelected()) {
            type = GSM_EU;
        }
    }//GEN-LAST:event_jRadioButton5ActionPerformed

    private void jRadioButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton6ActionPerformed
        if(jRadioButton6.isSelected()) {
            type = TELCEL;
        }
    }//GEN-LAST:event_jRadioButton6ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        jTextField11.setText("");
        if(okToGo()) {
            setState(false);
            t.schedule(new DoGenerate(), 50);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField3FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField3FocusGained
        if(!(jTextField3.getDocument() instanceof CustomFilter)) {
            jTextField3.setDocument(new CustomFilter(CustomFilter.NUMERIC));
        }
    }//GEN-LAST:event_jTextField3FocusGained

    private void jTextField4FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField4FocusGained
        if(!(jTextField4.getDocument() instanceof CustomFilter)) {
            jTextField4.setDocument(new CustomFilter(CustomFilter.NUMERIC));
        }
    }//GEN-LAST:event_jTextField4FocusGained

    private void jTextField2FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField2FocusGained
       if(!(jTextField2.getDocument() instanceof CustomFilter)) {
            jTextField2.setDocument(new CustomFilter(CustomFilter.NUMERIC));
        }
    }//GEN-LAST:event_jTextField2FocusGained

    private void jTextField1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField1FocusGained
        if(!(jTextField1.getDocument() instanceof CustomFilter)) {
            jTextField1.setDocument(new CustomFilter(CustomFilter.FLOAT));
        }
    }//GEN-LAST:event_jTextField1FocusGained

    private void jTextField6FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField6FocusGained
        if(!(jTextField6.getDocument() instanceof CustomFilter)) {
            jTextField6.setDocument(new CustomFilter(CustomFilter.NUMERIC));
        }
    }//GEN-LAST:event_jTextField6FocusGained

    private void jTextField5FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField5FocusGained
        if(!(jTextField5.getDocument() instanceof CustomFilter)) {
            jTextField5.setDocument(new CustomFilter(CustomFilter.NUMERIC));
        }
    }//GEN-LAST:event_jTextField5FocusGained

    private void jTextField7FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField7FocusGained
        if(!(jTextField7.getDocument() instanceof CustomFilter)) {
            jTextField7.setDocument(new CustomFilter(CustomFilter.NUMERIC));
        }
    }//GEN-LAST:event_jTextField7FocusGained

    private void jTextField8FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField8FocusGained
        if(!(jTextField8.getDocument() instanceof CustomFilter)) {
            jTextField8.setDocument(new CustomFilter(CustomFilter.NUMERIC));
        }
    }//GEN-LAST:event_jTextField8FocusGained

    private void jTextField9FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField9FocusGained
        if(!(jTextField9.getDocument() instanceof CustomFilter)) {
            jTextField9.setDocument(new CustomFilter(CustomFilter.NUMERIC));
        }
    }//GEN-LAST:event_jTextField9FocusGained

    private void jTextField10FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField10FocusGained
        if(!(jTextField10.getDocument() instanceof CustomFilter)) {
            jTextField10.setDocument(new CustomFilter(CustomFilter.NUMERIC));
        }
    }//GEN-LAST:event_jTextField10FocusGained

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if(jTextField11.getText().startsWith("http://")) {
            try {
                Desktop.getDesktop().browse(new URI(jTextField11.getText()));
            } catch(Exception e) {}
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup carriersGroup;
    private javax.swing.ButtonGroup devicesGroup;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JLayeredPane jLayeredPane3;
    private javax.swing.JLayeredPane jLayeredPane4;
    private javax.swing.JLayeredPane jLayeredPane5;
    private javax.swing.JLayeredPane jLayeredPane6;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JRadioButton jRadioButton6;
    private javax.swing.JRadioButton jRadioButton7;
    private javax.swing.JRadioButton jRadioButton8;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField11;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.ButtonGroup releaseDateGroup;
    // End of variables declaration//GEN-END:variables

    class DoGenerate extends TimerTask  {
        public void run() {
            version = jTextField1.getText();
            if(jRadioButton7.isSelected()) {
                mm = Integer.parseInt(jTextField3.getText());
                dd = Integer.parseInt(jTextField4.getText());
                yyyy = Integer.parseInt(jTextField2.getText());
                if(isValidDate(dd, mm, yyyy)) {
                    String url = buildURL();
                    if(url!=null) {
                        jTextField11.setText(url);
                    } else {
                        jTextField11.setText("Error: unable to build URL");
                    }
                } else {

                }
            } else { //jRadioButton8 is selected
                if(isRangeValid()) {
                    if(scanForDate()) {
                        JOptionPane.showMessageDialog(mainPanel, "Release date found: " +
                                MONTHS[mm-1] + " " + dd + ", " + yyyy);
                        jTextField3.setText(mm + "");
                        jTextField4.setText(dd + "");
                        jTextField2.setText(yyyy + "");
                        jTextField6.setText("");
                        jTextField5.setText("");
                        jTextField7.setText("");
                        jTextField8.setText("");
                        jTextField9.setText("");
                        jTextField10.setText("");
                        jRadioButton7.setSelected(true);
                        t.schedule(new DoGenerate(), 50);
                    } else {
                        JOptionPane.showMessageDialog(mainPanel, "Error: webOSDoctor " +
                                "not found in given date range");
                    }
                } else {
                    JOptionPane.showMessageDialog(mainPanel, "Error: date range is not" +
                            " valid");
                }
            }
            setState(true);
        }
    }

    public class CustomFilter extends PlainDocument {
        public static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
        public static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        public static final String ALPHA = LOWERCASE + UPPERCASE;
        public static final String NUMERIC = "0123456789";
        public static final String FLOAT = NUMERIC + ".";
        public static final String ALPHA_NUMERIC = ALPHA + NUMERIC;

        protected String acceptedChars = null;
        protected boolean negativeAccepted = false;

        public CustomFilter() {
            this(ALPHA_NUMERIC);
        }
        public CustomFilter(String acceptedchars) {
            acceptedChars = acceptedchars;
        }

        public void setNegativeAccepted(boolean negativeaccepted) {
            if (acceptedChars.equals(NUMERIC) ||
                    acceptedChars.equals(FLOAT) ||
                    acceptedChars.equals(ALPHA_NUMERIC)){
                negativeAccepted = negativeaccepted;
                acceptedChars += "-";
            }
        }

        @Override
        public void insertString(int offset, String  str, AttributeSet attr) throws BadLocationException {
            if (str == null) return;

            if (acceptedChars.equals(UPPERCASE))
                str = str.toUpperCase();
            else if (acceptedChars.equals(LOWERCASE))
                str = str.toLowerCase();

            for (int i=0; i < str.length(); i++) {
                if (acceptedChars.indexOf(String.valueOf(str.charAt(i))) == -1)
                    return;
            }

            if (negativeAccepted && str.indexOf("-") != -1) {
                if (str.indexOf("-") != 0 || offset != 0 ) {
                    return;
                }
            }

            super.insertString(offset, str, attr);
        }
    }
}
